package com.qingcheng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.qingcheng.dao.OrderMapper;
import com.qingcheng.dao.TradeReportMapper;
import com.qingcheng.pojo.order.Order;
import com.qingcheng.pojo.order.TradeReport;
import com.qingcheng.service.order.TradeReportService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 交易统计
 */
@Service
public class TradeReportServiceImpl implements TradeReportService {

    @Autowired
    private TradeReportMapper tradeReportMapper;

    /**
     * 创建交易统计数据
     */
    public void createData() {
//        LocalDate date = LocalDate.now().minusDays(1);//获得昨天的时间
        LocalDate date = LocalDate.of(2019,04,15);
        TradeReport tradeReport = new TradeReport();//创建一个空对象

        /**
         * 查询交易统计表所需要的属性
         */
        //查询: 下单人数,订单数 下单件数 下单金额 统计时间
        TradeReport tradeReport1 = tradeReportMapper.findOrderNumAndOrderTotalNumAndOrderMoney(date);
        System.out.println(tradeReport1.getOrderPeopleNum());
        //查询: 付款人数 付款订单数 付款件数 付款金额 客单价
        TradeReport tradeReport2 = tradeReportMapper.findPayPeopleNumAndPayOrderNumAndPayOrderTotalNumAndPayTotalMoneyAndPaySingleMoney(date);
        //查询: 有效订单数
        int effectiveOrderNum = tradeReportMapper.findEffectiveOrderNum(date);
        //查询: 退款金额
        int refundMoney = tradeReportMapper.findRefundMoney(date);
        //查询: 统计时间
        Date countDate = tradeReportMapper.findCountDate(date);

        //设置交易统计表所需要的属性
        tradeReport.setVisitPeopleNum(10000);//浏览人数 这个应该是百度统计查询出的
        tradeReport.setOrderPeopleNum(tradeReport1.getOrderPeopleNum());//下单人数
        tradeReport.setOrderNum(tradeReport1.getOrderNum());//订单数
        tradeReport.setOrderTotalNum(tradeReport1.getOrderTotalNum());//下单件数
        tradeReport.setEffectiveOrderNum(effectiveOrderNum);//有效订单数
        tradeReport.setOrderMoney(tradeReport1.getOrderMoney());//下单金额
        tradeReport.setRefundMoney(refundMoney);//退款金额
        tradeReport.setPayPeopleNum(tradeReport2.getPayPeopleNum());//付款人数
        tradeReport.setPayOrderNum(tradeReport2.getPayOrderNum());//付款订单数
        tradeReport.setPayOrderTotalNum(tradeReport2.getPayOrderTotalNum());//付款件数
        tradeReport.setPayTotalMoney(tradeReport2.getPayTotalMoney());//付款金额
        tradeReport.setPaySingleMoney(tradeReport2.getPaySingleMoney());//客单价
        tradeReport.setCountDate(countDate);//统计时间

        //插入到交易统计表中
        tradeReportMapper.insert(tradeReport);
    }

    /**
     * 报表服务层接口
     * @return
     */
    public List<Map> tradeReport(String date1,String date2){
        List<Map> tradeReportList = tradeReportMapper.tradeReport(date1, date2);
        return tradeReportList;
    }

    /**
     * 详细报表服务层接口
     * @return
     */
    public List<Map> tradeReport2(String date1,String date2){
        List<Map> tradeReportList = tradeReportMapper.tradeReport2(date1, date2);
        return tradeReportList;
    }
}
